const { Orders } = require('../models/orders');
const findByPaginationHandler = require('../utils/handlerPagination')


//获得订单列表
async function getOrderList(req, res, next) {
    try {
        const data = await findByPaginationHandler('orders', req.body);
        // console.log(req.body);
        res.send({ code: 1, data: { ...data, ...req.body } });
    } catch (error) {
        console.log("err", error);
        next(error);
    }

}

//删除一个订单
async function deleteOneOrder(req, res, next) {
    try {
        const { id } = req.query;
        console.log("id", id)
        const data = await Orders.findByIdAndUpdate(id, { status: 'delete' })
        res.send({ code: 1, data })
    } catch (error) {
        console.log("err", error)
    }
}

//获得一个订单的详情
async function getOneOrder(req, res, next) {
    try {
        const { id } = req.query;
        const data = await Orders.find({ _id: id }).populate("carId").populate('ownerId').populate('userId')
        res.send({
            code: 1,
            data: data[0]
        })
    } catch (error) {
        console.log("err", error);
        next(error);
    }

}

module.exports = {
    getOrderList,
    deleteOneOrder,
    getOneOrder
};